//
//  SectionListHeader.m
//  Doctor
//
//  Created by 阳光 on 16/10/9.
//  Copyright © 2016年 阳光. All rights reserved.
//

#import "SectionListHeader.h"

@interface SectionListHeader()

// 箭头
@property(strong,nonatomic)UIButton *arrowButton;

@end

@implementation SectionListHeader

+ (instancetype)headerView:(UITableView *)tableView
{
    static NSString *identifier=@"header";
    SectionListHeader *headerView=(SectionListHeader *)[tableView dequeueReusableCellWithIdentifier:identifier];
    if (!headerView){
        headerView=[[SectionListHeader alloc] initWithReuseIdentifier:identifier];
    }
    return headerView;
}

-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self=[super initWithReuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self addSubview:self.arrowButton];
      
        // 添加一条底线
        UIView *linesView=[[UIView alloc]initWithFrame:CGRectMake(0,selectViewH-2,APPW,2)];
        linesView.backgroundColor=MAIN_BACK_COLOR;
        [self addSubview:linesView];
        
    }
    return self;
}

#pragma mark - 懒加载
- (UIButton *)arrowButton
{
    if(!_arrowButton)
    {
        _arrowButton =[UIButton buttonWithType:UIButtonTypeCustom];
        [_arrowButton setBackgroundImage:[UIImage imageNamed:@"header_bg"] forState:UIControlStateNormal];
        [_arrowButton setBackgroundImage:[UIImage imageNamed:@"header_bg_highlighted"] forState:UIControlStateHighlighted];
        [_arrowButton setImage:[UIImage imageNamed:@"buddy_header_arrow.png"] forState:UIControlStateNormal];
        [_arrowButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        /** 设置Button按钮内容的内边距*/
        [_arrowButton setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        /** 设置Button内容的位置居左*/
        [_arrowButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        [_arrowButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
        /** 设置button图片的位置*/
        [_arrowButton setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
        [_arrowButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        _arrowButton.imageView.clipsToBounds = NO;
        _arrowButton.titleLabel.font=Font_Number(15);
        [_arrowButton setTitle:@"车当当" forState:UIControlStateNormal];
    }
    return _arrowButton;
}


#pragma mark - buttonAction
- (void)buttonAction
{
    self.sectionGroupModel.isOpen = !self.sectionGroupModel.isOpen;
    
    /** 如果代理响应了代理方法，就要调用这个方法*/
    if ([self.delegate respondsToSelector:@selector(didSelectTableViewHeaderFooterView)]) {
        [self.delegate didSelectTableViewHeaderFooterView];
    }
}

- (void)didMoveToSuperview
{
    /** 根据isOpen属性判断是否旋转？如果为YES，则旋转90°，否则不旋转*/
    self.arrowButton.imageView.transform = self.sectionGroupModel.isOpen ? CGAffineTransformMakeRotation(M_PI_2) : CGAffineTransformMakeRotation(0);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    /** 注意这里要设置为bounds 不然会出错*/
    self.arrowButton.frame = self.bounds;
}

#pragma mark - groupModel
-(void)setSectionGroupModel:(SectionGroupModel *)sectionGroupModel
{
    _sectionGroupModel=sectionGroupModel;
//    self.personNumberLabel.text=sectionGroupModel.personNumber;
    [self.arrowButton setTitle:sectionGroupModel.name forState:UIControlStateNormal];
}


@end
